<?php

//回收站

class recycleController extends baseController
{
    //显示列表
    public function indexAction()
    {
        $table_id = $this->common->Get('table_id');
        if ($table_id == ""){
            die("table_id 不能为空");
        }
        $filedModel = new filedModel();
        $filedlistJson = $filedModel->getFiledJsonByTableid($table_id);

        $filedSearchList = $filedModel->getsearchfiledByTableid($table_id);
        //会显示出来的字段
        $filedShowList = $filedModel->getshowfiledByTableid($table_id);
        //挂载  组件类
        $this->library('Component');
        $component = new Component();

        include CUR_VIEW_PATH . "Srecycle" . DS . "recycle_list.html";
    }

    //获取数据列表
    public function gettablelistbytableidAction(){

        $tableModel = new tableModel();
        $table_id = $this->common->Get('table_id');
        $tableName = $tableModel->getTablenameByTableid($table_id);
        $_tableModel = new Model($tableName);
        $page = $this->common->Get("page");
        $limit= $this->common->Get("limit");
        $page = ($page-1)*$limit;
        $table_sql = $tableModel->getSqlWhereStr_new($table_id);
        if($table_sql=="")
        {
            $table_sql = " 1=1";
        }
        if($table_id==40){
            $_tableList1 = $_tableModel->findBySql("select * from {$tableName} where {$table_sql} order by cipin desc,id desc limit {$page},{$limit} ");
        }else if($table_id==13 || $table_id==14 || $table_id==15){
            $_tableList1 = $_tableModel->findBySql("select * from {$tableName} where {$table_sql} order by fawenriqi desc,id desc limit {$page},{$limit} ");
        }else{
            $_tableList1 = $_tableModel->findBySql("select * from {$tableName} where {$table_sql} order by id desc limit {$page},{$limit} ");
        }
        //挂载控件类
        $this->library("Component");
        $ComponentClass = new Component();
        $filedModel=new filedModel();
        $filedAraay=$filedModel->getallFiledByTableid($table_id);
        //查询回收站文章标题
        $zhengcekuModule = new zhengcekuModule();               //政策库
        $zhengcejieduModule = new zhengcejieduModule();         //政策库解读
        $zhengcedongtaiModule = new zhengcedongtaiModule();     //政策库动态
        foreach ($filedAraay as $v)
        {
            //处理不同字段的显示数据
            foreach ($_tableList1 as $k1 => $v1) {
                switch ($v1["type"]){
                    case "13":  //政策库
                        $info = $zhengcekuModule->findOne($v1["data_id"]);
                        $v1["data_id"] = !empty($info) ? $info["biaoti"] : "";
                        break;
                    case "14":  //政策库解读
                        $info = $zhengcejieduModule->findOne($v1["data_id"]);
                        $v1["data_id"] = !empty($info) ? $info["biaoti"] : "";
                        break;
                    case "15":  //政策库动态
                        $info = $zhengcedongtaiModule->findOne($v1["data_id"]);
                        $v1["data_id"] = !empty($info) ? $info["biaoti"] : "";
                        break;
                }
                $v1[$v['u1']] = $ComponentClass->showKj($v['u7'],$v['u2'],$v['u1'] ,$v1[$v['u1']],$v['u3'] ,$v['id']) ;
                $_tableList1[$k1] = $v1;
            }
        }

        foreach ($_tableList1 as &$value){
            $value['sort_id'] = (new sortModel())->value(['id'=>$value['sort_id']])['u1'];
        }

        $_tableList["data"]=$_tableList1;
        $_tableList["code"]="0";
        $_tableList["msg"]="";
        $_tableList["count"]=count($_tableModel->findBySql("select * from {$tableName} where {$table_sql}  "));

        $this->common->ajaxReturn($_tableList);
    }

    //添加页面
    public function addAction()
    {
        $table_id = $this->common->Get('table_id');
        if ($table_id == "") {
            die("table_id 不能为空");
        }
        $filedModel = new filedModel();
        $filedList = $filedModel->getallFiledByTableid($table_id);
        $filedshowtypeList = $filedModel->getfiledShowTypeBytableid($table_id);

        $tableModel = new tableModel();
        $tableDetail = $tableModel->findOne($table_id);

        //挂载  组件类
        $this->library('Component');
        $component = new Component();
//        $component->getKj();
        include CUR_VIEW_PATH . "Srecycle" . DS . "recycle_add.html";
    }

    //编辑页面
    public function editAction()
    {
        $table_id = $this->common->Get('table_id');
        $id = $this->common->Get('id');
        if ($table_id == "") {
            die("table_id 不能为空");
        }
        $filedModel = new filedModel();
        $filedList = $filedModel->getallFiledByTableid($table_id);
        $filedshowtypeList = $filedModel->getfiledShowTypeBytableid($table_id);

        $tableModel = new tableModel();
        $tableDetail = $tableModel->findOne($table_id);
        $tablename = $tableModel->getTablenameByTableid($table_id);
        $_tableModel = new Model($tablename);
        $_tableDetail = $_tableModel->findOne($id);

        //挂载  组件类
        $this->library('Component');
        $component = new Component();


        include CUR_VIEW_PATH . "Srecycle" . DS . "recycle_edit.html";
    }

    //添加
    public function insertAction()
    {
        //获取该用户组信息
        $data_return["status"] = false;
        $data_return["msg"] = "";
        $data_return["code"] = 1;

        $table_id = $this->common->Requert('table_id');
        // 获得当前表名
        $tableModel = new tableModel();
        $tableName = $tableModel->getTablenameByTableid($table_id);
        //先获取文章信息
        $_tableModel = new Model($tableName);

        //1.收集表单数据
        $data = $_tableModel->getFieldArray();

        //2.验证和处理

        $this->helper('input');
        $data = deepspecialchars($data);
        $data = deepslashes($data);


        $filedModel = new filedModel();
        $filedAraay = $filedModel->getallFiledByTableid($table_id);


        //处理表单验证===>正则匹配
        foreach ($filedAraay as $v) {
            //设置了不为空
            if ($v['u4'] == "是") {
                if ($data[$v['u1']] == "" || $data[$v['u1']] == null) {
                    $data_return["msg"] = "操作失败:" . $v['u2'] . "不能为空";
                    $this->common->ajaxReturn($data_return);

                }

            }

            if (isset($v['u14']) && $GLOBALS['config_regular'][$v['u14']] != "无" && $GLOBALS['config_regular'][$v['u14']] != "") {
                $n = preg_match($GLOBALS['config_regular'][$v['u14']], $data[$v['u1']], $array);
                /*接下来的正则表达式("/131,132,133,135,136,139开头随后跟着任意的8为数字 '|'(或者的意思)
                 * 151,152,153,156,158.159开头的跟着任意的8为数字
                 * 或者是188开头的再跟着任意的8为数字,匹配其中的任意一组就通过了
                 * /")*/
                //var_dump($array); //看看是不是找到了,如果找到了,就会输出电话号码的
                if (count($array) == 0) {
                    //改字段不合法
                    $data_return["msg"] = $v['u2'] . "字段不合法";
                    $this->common->ajaxReturn($data_return);

                }
            }
        }


        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='时间框' and u8='CURRENT_TIMESTAMP' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0) {
                $data[$v['u1']] = date('Y-m-d H:i:s', time());
            }
        }


        //单独处理密码
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='密码' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0) {
                $data[$v['u1']] = md5($data[$v['u1']]);
            }
        }
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='文本编辑器' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0 && !empty($data[$v['u1']])) {
                $data[$v['u1']] = html_entity_decode($data[$v['u1']]);
            }
        }


        //3调用模型完成入库并给出提示
        if ($_tableModel->insert($data)) {
            $data_return["status"] = true;
            $data_return["msg"] = "添加成功";
            $data_return["code"] = 0;

        } else {
            $data_return["msg"] = "添加失败";

        }
        $this->common->ajaxReturn($data_return);

    }

    //编辑
    public function updateAction()
    {
        //获取该用户组信息
        $data_return["status"] = false;
        $data_return["msg"] = "";
        $data_return["code"] = 1;

        $id = $this->common->Requert('id');
        $table_id = $this->common->Requert('table_id');
        if ($id == "") {
            $data_return["msg"] = "id 不能为空";
            $this->common->ajaxReturn($data_return);
        }
        // 获得当前表名
        $tableModel = new tableModel();
        $tableName = $tableModel->getTablenameByTableid($table_id);
        //先获取文章信息
        $_tableModel = new Model($tableName);

        //1.收集表单数据
        $data = $_tableModel->getFieldArray();

        //2.验证和处理

        $this->helper('input');
        $data = deepspecialchars($data);
        $data = deepslashes($data);

        $filedModel = new filedModel();
        $filedAraay = $filedModel->getallFiledByTableid($table_id);


        //处理表单验证===>正则匹配
        foreach ($filedAraay as $v) {
            //设置了不为空
            if ($v['u4'] == "是") {
                if ($data[$v['u1']] == "" || $data[$v['u1']] == null) {
                    $data_return["msg"] = "操作失败:" . $v['u2'] . "不能为空";
                    $this->common->ajaxReturn($data_return);

                }

            }

            if (isset($v['u14']) && $GLOBALS['config_regular'][$v['u14']] != "无" && $GLOBALS['config_regular'][$v['u14']] != "") {
                $n = preg_match($GLOBALS['config_regular'][$v['u14']], $data[$v['u1']], $array);
                /*接下来的正则表达式("/131,132,133,135,136,139开头随后跟着任意的8为数字 '|'(或者的意思)
                 * 151,152,153,156,158.159开头的跟着任意的8为数字
                 * 或者是188开头的再跟着任意的8为数字,匹配其中的任意一组就通过了
                 * /")*/
                //var_dump($array); //看看是不是找到了,如果找到了,就会输出电话号码的
                if (count($array) == 0) {
                    //改字段不合法
                    $data_return["msg"] = $v['u2'] . "字段不合法";
                    $this->common->ajaxReturn($data_return);

                }
            }
        }


        //单独处理密码
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='密码' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0 && $data[$v['u1']] != "") {
                $data[$v['u1']] = md5($data[$v['u1']]);
            }
        }

        //var_dump($data);die();

        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='文本编辑器' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0 && !empty($data[$v['u1']])) {
                $data[$v['u1']] = html_entity_decode($data[$v['u1']]);
            }
        }


        //3调用模型完成入库并给出提示
        if ($_tableModel->update($data)) {
            $data_return["status"] = true;
            $data_return["msg"] = "更新成功";
            $data_return["code"] = 0;

        } else {
            $data_return["msg"] = "更新失败";

        }
        $this->common->ajaxReturn($data_return);

    }

    //删除
    public function deleteAction()
    {
        $data_return["status"] = true;
        $data_return["msg"] = "添加成功";
        $data_return["code"] = 0;

        $ids = $this->common->Requert('id');
        $table_id = $this->common->Requert('table_id');
        if ($ids == "") {
            $data_return["msg"] = "id 不能为空";
            $this->common->ajaxReturn($data_return);
        }
        if ($table_id == "") {
            $data_return["msg"] = "table_id不能为空";
            $this->common->ajaxReturn($data_return);
        }

        $tableModel = new tableModel();
        $tableName = $tableModel->getTablenameByTableid($table_id);

        $array_id = explode(",", $ids);
        $array_id = array_unique($array_id);

        $_tableModel = new Model($tableName);

        //开启事务执行删除
        $_tableModel->start_T();
        //删除正式数据
        $zhengcekuModule = new zhengcekuModule();               //政策库
        $zhengcejieduModule = new zhengcejieduModule();         //政策库解读
        $zhengcedongtaiModule = new zhengcedongtaiModule();     //政策库动态
        foreach ($array_id as $val){
            //查询回收站数据
            $recycle_info = $_tableModel->findOne($val);
            switch ($recycle_info["type"]){
                case "13":  //政策库
                    $zhengcekuModule->delete($recycle_info["data_id"]);
                    break;
                case "14":  //政策库解读
                    $zhengcejieduModule->delete($recycle_info["data_id"]);
                    break;
                case "15":  //政策库动态
                    $zhengcedongtaiModule->delete($recycle_info["data_id"]);
                    break;
            }
        }
        //删除回收站记录
        $res_recycle = $_tableModel->delete($array_id);
        if ($res_recycle) {
            $_tableModel->comit_T();
            $data_return["status"] = true;
            $data_return["msg"] = "删除成功";
            $data_return["code"] = 1;
        } else{
            $_tableModel->roll_T();
            $data_return["msg"] = "删除失败";
        }
        $this->common->ajaxReturn($data_return);

    }

    //恢复数据
    public function recycleAction()
    {
        $data_return["status"] = true;
        $data_return["msg"] = "恢复成功";
        $data_return["code"] = 0;

        $ids = $this->common->Requert('id');
        $table_id = $this->common->Requert('table_id');
        if ($ids == "") {
            $data_return["msg"] = "id 不能为空";
            $this->common->ajaxReturn($data_return);
        }
        if ($table_id == "") {
            $data_return["msg"] = "table_id不能为空";
            $this->common->ajaxReturn($data_return);
        }

        $tableModel = new tableModel();
        $tableName = $tableModel->getTablenameByTableid($table_id);

        $array_id = explode(",", $ids);
        $array_id = array_unique($array_id);

        $_tableModel = new Model($tableName);

        //开启事务
        $_tableModel->start_T();
        //修改正式数据status=1
        $zhengcekuModule = new zhengcekuModule();               //政策库
        $zhengcejieduModule = new zhengcejieduModule();         //政策库解读
        $zhengcedongtaiModule = new zhengcedongtaiModule();     //政策库动态
        foreach ($array_id as $val){
            //查询回收站数据
            $recycle_info = $_tableModel->findOne($val);
            switch ($recycle_info["type"]){
                case "13":  //政策库
                    $zhengcekuModule->where(["id"=>$recycle_info["data_id"]])->update(["status"=>1]);
                    break;
                case "14":  //政策库解读
                    $zhengcejieduModule->where(["id"=>$recycle_info["data_id"]])->update(["status"=>1]);
                    break;
                case "15":  //政策库动态
                    $zhengcedongtaiModule->where(["id"=>$recycle_info["data_id"]])->update(["status"=>1]);
                    break;
            }
        }
        //删除回收站记录
        $res_recycle = $_tableModel->delete($array_id);
        if ($res_recycle) {
            $_tableModel->comit_T();
            $data_return["status"] = true;
            $data_return["msg"] = "恢复成功";
            $data_return["code"] = 1;
        } else{
            $_tableModel->roll_T();
            $data_return["msg"] = "恢复失败";
        }
        $this->common->ajaxReturn($data_return);

    }

    //显示导入数据
    public function daoruAction()
    {
        $table_id = $this->common->Get('table_id');
        if ($table_id == "")
            die("table_id 不能为空");


        include CUR_VIEW_PATH . "Srecycle" . DS . "recycle_daoru.html";
    }

}